package com.sc.test;

import android.graphics.Color;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.DividerItemDecoration;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.recyclerview.widget.StaggeredGridLayoutManager;

import com.sc.test.recyclerview.DividerGridItemDecoration;
import com.sc.test.recyclerview.ItemDivider;
import com.sc.test.recyclerview.MyDividerItemDecoration;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by Shen Chao on 2018-01-13-013 下午 14:45.
 */

public class RecyclerActivity extends AppCompatActivity {
    @BindView(R.id.rv_view)
    RecyclerView rvView;
    private List<String> mDatas;
    private HomeAdapter mAdapter;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_recycler);
        ButterKnife.bind(this);
        initData();
    }

    private void initData() {
        mDatas = new ArrayList<>();
        for (int i = 1; i < 51; i++) {
            mDatas.add("" + i);
        }
    }

    @OnClick({R.id.bt_rv_linearlayout, R.id.bt_rv_gridlayout, R.id.bt_rv_staggeredgridlayout})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.bt_rv_linearlayout:
                rvView.setLayoutManager(new LinearLayoutManager(this));
                rvView.setAdapter(mAdapter = new HomeAdapter());
//                rvView.addItemDecoration(new MyDividerItemDecoration(this, MyDividerItemDecoration.VERTICAL_LIST));
                //设置分割线 v7  25以上版本官方已实现
                //样式可以在style里listdivider设置
                rvView.addItemDecoration(new DividerItemDecoration(this, DividerItemDecoration.VERTICAL));
                break;
            case R.id.bt_rv_gridlayout:
                rvView.setLayoutManager(new GridLayoutManager(this, 4));
                rvView.setAdapter(mAdapter = new HomeAdapter());
                //两个recyclerview切换，先点gridview再点linearlayout会报异常
//                rvView.addItemDecoration(new ItemDivider().setDivederWith(20).setDividerColor(Color.BLUE));
                rvView.addItemDecoration(new DividerGridItemDecoration(this));
                break;
            case R.id.bt_rv_staggeredgridlayout:
                rvView.setLayoutManager(new StaggeredGridLayoutManager(4, StaggeredGridLayoutManager.HORIZONTAL));
                rvView.setAdapter(mAdapter = new HomeAdapter());
                break;
        }
    }

    class HomeAdapter extends RecyclerView.Adapter<HomeAdapter.MyViewHolder> {

        @Override
        public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            MyViewHolder holder = new MyViewHolder(LayoutInflater.from(RecyclerActivity.this)
                    .inflate(R.layout.activity_recycler_item, parent, false));
            return holder;
        }

        @Override
        public void onBindViewHolder(MyViewHolder holder, int position) {
            holder.tv.setText(mDatas.get(position));
        }

        @Override
        public int getItemCount() {
            return mDatas.size();
        }

        class MyViewHolder extends RecyclerView.ViewHolder {
            @BindView(R.id.tv_recycler_item)
            TextView tv;

            public MyViewHolder(View itemView) {
                super(itemView);
                ButterKnife.bind(this, itemView);
            }
        }
    }
}
